﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EFmodel;
using IEFbll;
using EF.Container;

namespace EFdemo.Controllers
{
    public class UserController : Controller
    {
        private IUserService UserService = Container.Resolve<IUserService>();
        public ActionResult Index()
        {
            List<User> list = UserService.GetModels( p => true ).ToList();
            return View( list );
        }
        
        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="user">用户信息</param>
        /// <returns>重新刷新Index页面</returns>
        public ActionResult Add(User user)
        {
            if( UserService.Add( user ) )
            {
                return Redirect( "Index" );
            }
            else
            {
                return Content( "no" );
            }
        }
        public ActionResult Update(User user)
        {
            if( UserService.Update( user ) )
            {
                return Redirect( "Index" );
            }
            else
            {
                return Content( "no" );
            }
        }
        public ActionResult Delete(int Id)
        {
            var user = UserService.GetModels( p => p.Id == Id ).FirstOrDefault();
            if( UserService.Delete( user ) )
            {
                return Redirect( "Index" );
            }
            else
            {
                return Content( "no" );
            }
        }
    }
}